package com.tust.service;

import com.tust.bean.StudentCourse;
import com.tust.dao.StudentCourseDao;

import java.sql.Connection;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

public class ScoreServiceImpl implements BaseService{

    @Override
    public void service(Connection connection) {

        Scanner scanner = new Scanner(System.in);
        String type;

        List<StudentCourse> list;

        while (true) {
            System.out.println("请输入成绩管理操作命令：add->添加，edit->编辑,del->删除,Query->查询，QUIT->退出");
            StudentCourseDao studentCourseDao = new StudentCourseDao(connection);
            type = scanner.next();

            if (type.equalsIgnoreCase("add")) {

                list = studentCourseDao.queryScore();
                print(list);

                System.out.println("请依次单独输入要添加课程成绩所在行的id、课程分数：");
                StudentCourse studentCourse = new StudentCourse();
                studentCourse.setScid(scanner.nextInt());
                studentCourse.setScore(scanner.next());
                if (studentCourseDao.insertScore(studentCourse)) {
                    System.out.println("添加成功");
                    list = studentCourseDao.queryScore();
                    print(list);
                } else {
                    System.out.println("添加失败");
                }
            } else if (type.equalsIgnoreCase("del")) {

                // 打印成绩单
                list = studentCourseDao.queryScore();
                print(list);

                System.out.println("删除指定课程成绩请输入要删除课程成绩所在行的id、删除所有课程成绩请输入all：");
                type = scanner.next();
                if (type.equalsIgnoreCase("all")) {
                    if (studentCourseDao.deleteScore()) {
                        System.out.println("删除所有课程成绩成功");
                    } else {
                        System.out.println("删除所有课程成绩失败");
                    }
                } else {
                    StudentCourse studentCourse = new StudentCourse();
                    studentCourse.setScid(Integer.parseInt(type));
                    if (studentCourseDao.deleteScore(studentCourse)) {
                        System.out.println("删除指定课程成绩成功");
                        list = studentCourseDao.queryScore();
                        print(list);
                    } else {
                        System.out.println("删除制定课程成绩失败");
                    }
                }
            } else if (type.equalsIgnoreCase("edit")) {
                // 打印成绩单
                list = studentCourseDao.queryScore();
                print(list);

                System.out.println("请依次单独输入要更新课程成绩所在行的id、成绩：");
                StudentCourse studentCourse = new StudentCourse();
                studentCourse.setScid(scanner.nextInt());
                studentCourse.setScore(scanner.next());
                if (studentCourseDao.updateScore(studentCourse)) {
                    System.out.println("更新成功");
                    list = studentCourseDao.queryScore();
                    print(list);
                } else {
                    System.out.println("更新失败");
                }
            } else if (type.equalsIgnoreCase("query")) {
                System.out.println("查询指定学生成绩请输入学生学号，查询指定课程成绩请输入课程号，查询所有学生输入all。");
                type = scanner.next();
                if (type.equalsIgnoreCase("all")) {
                    list = studentCourseDao.queryScore();
                    print(list);
                } else if (canConvertToInt(type)) {
                    list = studentCourseDao.queryScore(Integer.parseInt(type));
                    print(list);
                } else  {
                    list = studentCourseDao.queryScore(type);
                    print(list);
                }
            } else if (type.equalsIgnoreCase("QUIT")) {
                studentCourseDao.close();
                break;
            }
        }
    }

    @Override
    public void print(List list) {
        if (list.size() > 0) {
            Iterator<StudentCourse> iterator = list.iterator();
            System.out.println("id\t学生学号\t  教师号\t 教师名\t 课程号\t 课程名\t\t分数");
            while (iterator.hasNext()) {
                StudentCourse studentCourse = iterator.next();
                String id = String.format("%-4s", studentCourse.getScid());
                String sid = String.format("%-10s", studentCourse.getSid());
                String tid = String.format("%-8s", studentCourse.getTid());
                String tname = String.format("%-6s", studentCourse.getTname());
                String cid = String.format("%-8s", studentCourse.getCid());
                String cname = String.format("%-10s", studentCourse.getCname());
                String score = String.format("%-4s", studentCourse.getScore());
                System.out.println(id + sid + tid + tname + cid + cname + score);
            }
        } else {
            System.out.println("无选课记录，请录入！");
        }
    }

    public static boolean canConvertToInt(String str) {
        try {
            Integer.parseInt(str);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }

}
